package com.panicstyle.Gongdong;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SetView extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setview);    

        findViewById(R.id.okbtn).setOnClickListener(mClickListener);
        findViewById(R.id.cancelbtn).setOnClickListener(mClickListener);
    }
    
    public void SaveData() {
    	
    	EditText textID = (EditText)findViewById(R.id.editID);
    	EditText textPW = (EditText)findViewById(R.id.editPW);

    	String userID = textID.getText().toString();    	
    	String userPW = textPW.getText().toString();    	

    	String msg = userID + " " + userPW;
    
    	Toast.makeText(this,  msg, Toast.LENGTH_SHORT).show();
    	String fileName = "LoginInfo.xml";
/*    	
        File newxmlfile = new File("LoginInfo.xml");
        try{
                newxmlfile.createNewFile();
        }catch(IOException e){
                Log.e("IOException", "exception in createNewFile() method");
                System.out.println(e.getMessage());
        }
        //we have to bind the new file with a FileOutputStream
       
*/
        FileOutputStream fileos = null;        
        try{
                fileos = openFileOutput(fileName, Context.MODE_PRIVATE);
        }catch(FileNotFoundException e){
                Log.e("FileNotFoundException", "can't create FileOutputStream");
                System.out.println(e.getMessage());
        }
        //we create a XmlSerializer in order to write xml data
        XmlSerializer serializer = Xml.newSerializer();
        try {
      
        	// we set the FileOutputStream as output for the serializer, using UTF-8 encoding
        	serializer.setOutput(fileos, "UTF-8");
            //Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
            serializer.startDocument(null, Boolean.valueOf(true));
           //set indentation option
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
            //start a tag called "root"
            serializer.startTag(null, "LoginInfo");
            //i indent code just to have a view similar to xml-tree
   
            serializer.startTag(null, "ID");
            //set an attribute called "attribute" with a "value" for 
//            serializer.attribute(null, "data", userID);
            serializer.text(userID);
            serializer.endTag(null, "ID");            
            
            serializer.startTag(null, "Password");
            //set an attribute called "attribute" with a "value" for 
//            serializer.attribute(null, "data", userPW);
            serializer.text(userPW);
            serializer.endTag(null, "Password");            
            
            serializer.endTag(null, "LoginInfo");
            serializer.endDocument();
            //write xml data into the FileOutputStream
            serializer.flush();
            //finally we close the file stream
            fileos.close();
                       
            } catch (Exception e) {
            	Log.e("Exception","error occurred while creating xml file");
            }
    	finish();
    }
    
    public void CancelData() {
    	Toast.makeText(this,  "CancelData", Toast.LENGTH_SHORT).show();
    	finish();
    }
    
    Button.OnClickListener mClickListener = new View.OnClickListener()
    {
      public void onClick(View v)
      {
           switch (v.getId())
           {
           case R.id.okbtn:
                SaveData();
                break;
           case R.id.cancelbtn:
                CancelData();
                break;
           }
      }
    };
}
